#!/bin/bash

# 自动挂载Sun卷脚本
# 将在开机时自动运行并挂载外部卷

# 设置配置主目录
if [ -z "$CONFIG_HOME" ]; then
  export CONFIG_HOME="${HOME}/.mac_sync"
fi

# 设置基本变量
VOLUME_NAME="Sun"
VOLUME_PATH="/Volumes/$VOLUME_NAME"
VOLUME_PASSWORD="041320" # 密码已更新
LOG_FILE="${CONFIG_HOME}/logs/auto_mount_$(date +%Y%m%d).log"
MAX_RETRIES=5
RETRY_DELAY=2

# 确保日志目录存在
mkdir -p "${CONFIG_HOME}/logs"

# 记录日志函数
log_message() {
    echo "[$(date +%Y-%m-%d\ %H:%M:%S)] $1" >> "$LOG_FILE"
    echo "$1"
}

log_message "开始自动挂载脚本"

# 备份Dock配置 - 新增：保护用户的Dock配置
DOCK_BACKUP_DIR="${CONFIG_HOME}/backup/dock"
mkdir -p "$DOCK_BACKUP_DIR"
DOCK_BACKUP_FILE="$DOCK_BACKUP_DIR/com.apple.dock.plist.$(date +%Y%m%d-%H%M%S)"
if [ -f "$HOME/Library/Preferences/com.apple.dock.plist" ]; then
    log_message "备份Dock配置到 $DOCK_BACKUP_FILE"
    cp "$HOME/Library/Preferences/com.apple.dock.plist" "$DOCK_BACKUP_FILE"
fi

# 等待系统完全启动
sleep 3

# 检查卷是否已挂载
if [ -d "$VOLUME_PATH" ]; then
    log_message "$VOLUME_NAME 卷已挂载，无需操作"
else
    log_message "$VOLUME_NAME 卷未挂载，尝试挂载"
    
    # 尝试找到卷的设备标识符
    disk_info=$(diskutil list | grep "$VOLUME_NAME")
    if [ -z "$disk_info" ]; then
        log_message "未找到 $VOLUME_NAME 卷，尝试使用APFS列表查找"
        disk_info=$(diskutil apfs list | grep -A 5 "$VOLUME_NAME")
    fi
    
    # 从输出中提取设备标识符
    disk_id=$(echo "$disk_info" | grep "disk" | awk '{print $1}' | head -1)
    
    if [ -z "$disk_id" ]; then
        log_message "无法找到 $VOLUME_NAME 卷的设备标识符"
        osascript -e 'display notification "无法找到外部卷，请手动检查" with title "挂载失败"'
        exit 1
    fi
    
    log_message "找到卷的设备标识符: $disk_id"
    
    # 检查是否需要解锁卷
    is_locked=$(diskutil info "$disk_id" | grep -i "locked" | grep -i "yes")
    if [ -n "$is_locked" ]; then
        log_message "卷已加密且已锁定，尝试解锁"
        
        # 使用echo传递密码，避免明文显示在进程列表中
        echo "$VOLUME_PASSWORD" | diskutil apfs unlockVolume "$disk_id" -stdinpass
        
        if [ $? -ne 0 ]; then
            log_message "解锁卷失败，密码可能不正确"
            osascript -e 'display notification "解锁外部卷失败，密码可能不正确" with title "挂载失败"'
            exit 1
        fi
        
        log_message "卷解锁成功"
    fi
    
    # 尝试挂载卷
    diskutil mount "$disk_id"
    
    if [ $? -ne 0 ]; then
        log_message "挂载卷失败"
        osascript -e 'display notification "挂载外部卷失败，请手动检查" with title "挂载失败"'
        exit 1
    fi
    
    log_message "卷挂载成功"
    
    # 等待挂载完成
    sleep 2
    
    # 检查挂载是否成功
    if [ ! -d "$VOLUME_PATH" ]; then
        log_message "挂载点未找到，挂载可能失败"
        osascript -e 'display notification "外部卷可能未正确挂载" with title "挂载警告"'
        exit 1
    fi
    
    log_message "确认挂载成功: $VOLUME_PATH 可访问"
fi

# 检查并修复损坏的应用链接
log_message "检查损坏的应用链接"
broken_links=0
fixed_links=0

for link in /Applications/*.app; do
    if [ -L "$link" ] && [ ! -e "$link" ]; then
        app_name=$(basename "$link")
        target_path="$VOLUME_PATH/Applications/$app_name"
        
        if [ -e "$target_path" ]; then
            log_message "修复链接: $app_name"
            rm "$link"
            ln -s "$target_path" "$link"
            fixed_links=$((fixed_links + 1))
        else
            log_message "无法修复链接 $app_name: 目标不存在"
        fi
        
        broken_links=$((broken_links + 1))
    fi
done

log_message "发现 $broken_links 个损坏链接，已修复 $fixed_links 个"

# 如果修复了链接，安全地刷新Dock图标缓存 - 修改：安全刷新，不影响分组
if [ $fixed_links -gt 0 ]; then
    log_message "安全刷新Dock图标缓存，保留用户配置"
    
    # 使用更安全的方法刷新图标缓存，不会影响Dock分组
    touch /Applications/*.app
    killall -KILL Dock
    
    log_message "Dock已重启，保留了用户的分组设置"
fi

log_message "自动挂载脚本完成"

exit 0 